package pers.supeijin.devtools.converter.wx2vue.converter.script;

import com.intellij.lang.javascript.psi.JSObjectLiteralExpression;
import com.intellij.lang.javascript.psi.JSProperty;

import java.util.HashMap;

/**
 * 声明周期转换辅助工具
 *
 * @date 2021/1/29.
 */

public class LifeCycleConvertUtils {
	private static final HashMap<String, String> mConvertMap = new HashMap<>();
	static {
		initConvertMap();
	}

	/**
	 * 初始化生命周期转换关系
	 */
	private static void initConvertMap() {
		mConvertMap.put("onLoad", "created");
		mConvertMap.put("onReady", "mounted");
		mConvertMap.put("onShow", "updated");
		mConvertMap.put("onHide", "");
		mConvertMap.put("onUnload", "destroyed");

		mConvertMap.put("attached", "beforeCreate");
		//	mConvertMap.put("created","created");
		mConvertMap.put("ready", "mounted");
		mConvertMap.put("detached", "beforeDestroy");
		mConvertMap.put("detached", "destroyed");
	}

	/**
	 * 转换生命周期属性函数
	 * @param jsObjectLiteralExpression
	 */
	public static void convertLifeCycleProperties(JSObjectLiteralExpression jsObjectLiteralExpression) {
		if (jsObjectLiteralExpression == null) {
			return;
		}
		JSProperty[] properties = jsObjectLiteralExpression.getProperties();
		if (properties.length < 1) {
			return;
		}
		for (JSProperty property : properties) {
			String propertyName = property.getName();
			String convertName = mConvertMap.getOrDefault(propertyName, null);
			if(convertName!=null){
				property.setName(convertName);
			}
		}
	}
}
